# -*- coding: UTF-8 -*- #
"""
@filename:01-7几何变换的基本知识.py
@author:JiYujia
@time:2022-09-05
"""

import cv2
import numpy as np

img = cv2.imread('pics/car.png')
imginfo = img.shape
h = imginfo[0]
w = imginfo[1]
deep = imginfo[2]

# 平移
# trans = np.array([[1, 0, 100], [0, 1, 100]], np.float32)
# dst = cv2.warpAffine(img, trans, (w, h))

# 缩放
# 方法1 仿射变换
# mat = np.array([[0.5, 0, 0], [0, 0.5, 0]], np.float32)
# dst = cv2.warpAffine(img, mat, (w, h))

# 方法2 resize函数
# dst = cv2.resize(img, (int(w / 2), int(h / 2)))

# 旋转 使用rotate函数
# rotate = cv2.getRotationMatrix2D((w / 2, h / 2), 60, 0.5)
# dst = cv2.warpAffine(img, rotate, (w, h))

# 裁剪
# dst = img[300:500, 200:400]

# 仿生变换
# 原图变换的顶点
# pts2 = np.float32([[50, 50], [100, 50], [50, 100]])
# # 目标图像变换顶点
# pts1 = np.float32([[100, 100], [200, 50], [100, 200]])
# # 构建仿射变换描述矩阵
# M = cv2.getAffineTransform(pts1, pts2)
# # 进行仿射变换
# dst = cv2.warpAffine(img, M, (w, h))

# 镜像
# 创建一个窗体
newimginfo = (h, w * 2, deep)
dst = np.zeros(newimginfo, np.uint8)
# 将图像分别前到后，后到前绘制
for i in range(0, h):
    for j in range(0, w):
        dst[i, j] = img[i, j]
        dst[i, w * 2 - j - 1] = img[i, j]
# 绘制中心分割线
for i in range(0, h):
    dst[i, w] = (0, 0, 255)

cv2.imshow('src image', img)
cv2.imshow('dst image', dst)

cv2.waitKey()
